<template>
  <div>
    <div class="top">
      <van-nav-bar fixed>
        <template #left>
          <van-icon name="medal-o" size="0.8rem" color="#fff" />
        </template>
        <template #right>
          <van-icon name="search" size="0.48rem" @click="goSearch" color="#fff" />
        </template>
      </van-nav-bar>
    </div>
    <div class="main">
      <van-tabs v-model="channelId" sticky offset-top="1.22667rem" animated>
        <van-tab
          v-for="item in userChannelsList"
          :key="item.id"
          :title="item.name"
          :name="item.id"

        >
          <article-list :channelId="channelId"></article-list>
        </van-tab>
      </van-tabs>
      <van-icon
        name="plus"
        size="0.37333334rem"
        class="more"
        @click="showPopup"
        
      ></van-icon>
    </div>
    <van-popup
      v-model="show"
      position="left"
      closeable
      :style="{ height: '100%', width: '100%' }"
    >
      <channel-edit
        :userChannelsList="userChannelsList"
        :unCheckChannel="checkChannel"
        @addChannel="addChannel"
        @removeChannel="removeChannel"
        v-model="channelId"
      ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import {
  getUserChannels,
  getAllChannels,
  updateChannel,
  deleteUserChannel,
} from "../../api/index";
import articleList from "./components/articleList.vue";
import channelEdit from "./components/channelEdit.vue";
export default {
  name: "home",
  components: {
    articleList,
    channelEdit,
  },
  data() {
    return {
      channelId: 0,
      userChannelsList: [],
      //   articleList: [],
      show: false,
      allChannels: [],
    };
  },
  async created() {
    let res = await getUserChannels();
    this.userChannelsList = res.data.data.channels;
    // this.changeChannel()
    let res2 = await getAllChannels();
    this.allChannels = res2.data.data.channels;
    // console.log(res2);
  },
  //   每次数据请求会导致页面数据重新刷新，数据不能够保持一致，进行优化
  //   methods: {
  //     async changeChannel() {
  //       let res2 = await getArticleList({
  //         channel_id: this.channelId,
  //         timestamp: +new Date(),
  //       });
  //       this.articleList = res2.data.data.results;
  //     },
  //   },

  methods: {
    showPopup() {
      this.show = true;
    },
    async addChannel(val) {
      this.userChannelsList.push(val);
      // 推荐频道不能传
      const newArr = this.userChannelsList.filter((item) => item !== 0);
      let theNewArr = newArr.map((item, index) => {
        const newObj = { ...item };
        delete newObj.name;
        newObj.seq = index;
        return newObj;
      });
      await updateChannel(theNewArr);
    },
    async removeChannel(val) {
      let index = this.userChannelsList.findIndex((item) => item.id === val.id);
      this.userChannelsList.splice(index, 1);
      await deleteUserChannel(val.id);
    },
    goSearch(){
      this.$router.push({name:'search'})
    }
  },
  computed: {
    // 查找所有频道，对用户已有频道比较，通过filter没有添加
    checkChannel() {
      const newArr = this.allChannels.filter((item) => {
        let index = this.userChannelsList.findIndex((i) => {
          return item.id === i.id;
        });
        if (index > 1) {
          return false;
        } else {
          return true;
        }
      });
      return newArr;
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  margin-top: 44px;
  padding-bottom: 50px;
  /deep/.van-tabs__wrap {
    padding-right: 30px;
    background-color: #fff;
  }
  .more {
    position: fixed;
    top: 60px;
    right: 8px;
    z-index: 999;
    padding: 0;
  }
}
</style>